<template>
  <div class="pagination">
    <button @click="$emit('getPage',pageNo-1)" :disabled="pageNo==1">上一页</button>
    <button @click="$emit('getPage',1)" v-if="startAndEnd.start!=1" :class="{active:pageNo==1}">1</button>
    <button v-if="startAndEnd.start>2">···</button>

    <button v-for="(page,index) in startAndEnd.end" :key="index" v-show="page>=startAndEnd.start" @click="$emit('getPage',page)" :class="{active:page==pageNo}">{{page}}</button>
    
    <button v-if="startAndEnd.end<pageTotal-1">···</button>
    <button v-if="startAndEnd.end!=pageTotal" @click="$emit('getPage',pageTotal)" :class="{active:pageNo==pageTotal}">{{pageTotal}}</button>
    <button @click="$emit('getPage',pageNo-1)" :disabled="pageNo==pageTotal">下一页</button>
    
    <button style="margin-left: 30px">共{{pageTotal}}条</button>
  </div>
</template>

<script>
  export default {
    name: "Pagination",
    props:['pageSize','pageNo','total','continue'],
    // 分页器
    computed:{
        pageTotal(){
            return Math.ceil(this.total/this.pageSize)
        },
        // 计算分页
        startAndEnd(){
            let start=0
            let end=0
            //情况一：如果总页数不够5页
            if(this.pageTotal<5){
                start=1
                end=this.pageTotal

            }else{
                start=this.pageNo-parseInt(this.continue/2)
                end=this.pageNo+parseInt(this.continue/2)
                // 情况二：如果一开始的页数<3，也就是说start<1的情况
                if(start<1){
                    start=1
                    end=this.continue
                }
                // 情况三:如果最页数在31页，也就是说end>总页数
                if(end>this.pageTotal){
                    start=this.pageTotal-this.continue+1
                    end=this.pageTotal
                }
            }
            return{start,end}

        }
    }
  }
</script>

<style lang="less" scoped>
  .pagination {
    text-align: center;
    button {
      margin: 0 5px;
      background-color: #f4f4f5;
      color: #606266;
      outline: none;
      border-radius: 2px;
      padding: 0 4px;
      vertical-align: top;
      display: inline-block;
      font-size: 13px;
      min-width: 35.5px;
      height: 28px;
      line-height: 28px;
      cursor: pointer;
      box-sizing: border-box;
      text-align: center;
      border: 0;

      &[disabled] {
        color: #c0c4cc;
        cursor: not-allowed;
      }

      &.active {
        cursor: not-allowed;
        background-color: #409eff;
        color: #fff;
      }
    }
  }
</style>